﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using H2.NET.Common;
using H2.NET.Data.Common;
using H2.NET.Web.Pages.SMD.Model;

namespace H2.NET.Web.Pages.SMD.Service
{
    public static class DepositService
    {
        public static t_smd_Deposit GetDeposit(int id)
        {
            return (t_smd_Deposit._DepositID == id).ListFirst<t_smd_Deposit>(Storage.Context3);
        }
        public static t_smd_Deposit GetDepositByStoreId(int storeid)
        {
            return (t_smd_Deposit._StoreID == storeid).ListFirst<t_smd_Deposit>(Storage.Context3);
        }
        public static t_smd_Deposit GetDeviceDeposit(int storeid, int deviceid)
        {
            return (t_smd_Deposit._StoreID == storeid
                    & t_smd_Deposit._DeviceID == deviceid
                    & t_smd_Deposit._DepositType == "设备保证金").ListFirst<t_smd_Deposit>(Storage.Context3,
                        " DepositID DESC");
        }

        public static t_smd_Deposit GetDeviceDeposit(int deviceid)
        {
            return (t_smd_Deposit._DeviceID == deviceid
                    & t_smd_Deposit._DepositType == "设备保证金").ListFirst<t_smd_Deposit>(Storage.Context3,
                        " DepositID DESC");
        }

        public static t_smd_Deposit GetDepostByStoreid(int storeid) 
        {
            return (t_smd_Deposit._StoreID == storeid
                & t_smd_Deposit._DepositType == "履约保证金").ListFirst<t_smd_Deposit>(Storage.Context3, " DepositID DESC");
        }

        public static void SaveDevice(t_smd_Store store, bool isConfirm, decimal amount)
        {
            var deviceDeposit = (t_smd_Deposit._AuthID == store.AuthID
                            & t_smd_Deposit._DepositType == "设备保证金"
                                & t_smd_Deposit._StoreID == store.StoreID).ListFirst<t_smd_Deposit>(Storage.Context3);
            if (deviceDeposit == null)
            {
                deviceDeposit = new t_smd_Deposit();
            }
            deviceDeposit.AuthID = store.AuthID;
            deviceDeposit.StoreID = store.StoreID;
            deviceDeposit.Amount = amount;
            deviceDeposit.DepositType = "设备保证金";
            if (isConfirm)
            {
                deviceDeposit.PostingDate = DateTime.Now;
            }
            Storage.Save(Storage.Context3, deviceDeposit);
        }

        //批量保存设备Sap
        public static void SaveDevice(t_smd_Store store, bool isConfirm, decimal amount, string sap)
        {
            var deviceDeposit = (t_smd_Deposit._AuthID == store.AuthID
                            & t_smd_Deposit._DepositType == "设备保证金"
                                & t_smd_Deposit._StoreID == store.StoreID).ListFirst<t_smd_Deposit>(Storage.Context3);
            if (deviceDeposit == null)
            {
                deviceDeposit = new t_smd_Deposit();
            }
            deviceDeposit.AuthID = store.AuthID;
            deviceDeposit.StoreID = store.StoreID;
            deviceDeposit.DepositType = "设备保证金";
            deviceDeposit.Amount = amount;
            deviceDeposit.SapDeviceNo = sap;
            if (isConfirm)
            {
                deviceDeposit.PostingDate = DateTime.Now;
            }
            Storage.Save(Storage.Context3, deviceDeposit);
        }

        public static void SaveRenovation(t_smd_Store store, bool isConfirm, decimal amount)
        {
            var agreeDeposit = (t_smd_Deposit._AuthID == store.AuthID
                            & t_smd_Deposit._DepositType == "履约保证金"
                                & t_smd_Deposit._StoreID == store.StoreID).ListFirst<t_smd_Deposit>(Storage.Context3);
            if (agreeDeposit == null)
            {
                agreeDeposit = new t_smd_Deposit();
            }
            agreeDeposit.AuthID = store.AuthID;
            agreeDeposit.StoreID = store.StoreID;
            agreeDeposit.Amount = amount;
            agreeDeposit.DepositType = "履约保证金";
            if (isConfirm)
            {
                agreeDeposit.PostingDate = DateTime.Now;
            }
            Storage.Save(Storage.Context3, agreeDeposit);
        }

        //批量审批履约SAp
        public static void SaveRenovation(t_smd_Store store, bool isConfirm, decimal amount,string sap)
        {
            var agreeDeposit = (t_smd_Deposit._AuthID == store.AuthID
                            & t_smd_Deposit._DepositType == "履约保证金"
                                & t_smd_Deposit._StoreID == store.StoreID).ListFirst<t_smd_Deposit>(Storage.Context3);
            if (agreeDeposit == null)
            {
                agreeDeposit = new t_smd_Deposit();
            }
            agreeDeposit.AuthID = store.AuthID;
            agreeDeposit.StoreID = store.StoreID;
            agreeDeposit.Amount = amount;
            agreeDeposit.SapDocumentNo = sap;
            agreeDeposit.DepositType = "履约保证金";
            if (isConfirm)
            {
                agreeDeposit.PostingDate = DateTime.Now;
            }
            Storage.Save(Storage.Context3, agreeDeposit);
        }

        public static List<v_DealerDeposit> FindByDealerIdAndStoreIds(int dealerId, List<string> storeid_arr)
        {
            Expression expression = new Expression();
            expression = v_DealerDeposit._DealerID == dealerId;
            if (storeid_arr != null && storeid_arr.Count > 0)
            {
                expression &= v_DealerDeposit._StoreID.In(storeid_arr);
            }
            return expression.List<v_DealerDeposit>(Storage.Context3).ToList();
        }
    }
}